//
//  ModifyPasswordViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddModifyPasswordViewController.h"
#import "JddViewControllerManager.h"

@interface JddModifyPasswordViewController ()

@end

@implementation JddModifyPasswordViewController

@synthesize oldPwdTextField;
@synthesize pwdTextField;
@synthesize backButton;
@synthesize confirmPwdTextField;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddModifyPasswordViewController);

- (void)ModifyPasswordOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    
    if (result) {
        if ([JddDataParser parseModifyPasswordResult:result]) {
            [JddUserProfile sharedInstance].password = pwdTextField.text;
            [[JddViewControllerManager sharedInstance] popToBackViewController];
        }
        [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
    }
}

-(IBAction)editOver{
    [oldPwdTextField resignFirstResponder];
    [pwdTextField resignFirstResponder];
    [confirmPwdTextField resignFirstResponder];
    
}

- (IBAction)submitButtonClicked:(id)sender{
    if (![oldPwdTextField.text length]) {
        [JddCommonFunction messageBoxWithMessage:@"请输入原密码"];
        return;
    }
    if (![pwdTextField.text length]) {
        [JddCommonFunction messageBoxWithMessage:@"新密码不能为空"];
        return;
    }
    if (![confirmPwdTextField.text length]) {
        [JddCommonFunction messageBoxWithMessage:@"确认密码不能为空"];
        return;
    }
    if (![oldPwdTextField.text isEqualToString:[JddUserProfile sharedInstance].password] ) {
        [JddCommonFunction messageBoxWithMessage:@"原密码输入不正确"];
        return;
    }
    if (![pwdTextField.text isEqualToString:confirmPwdTextField.text]) {
        [JddCommonFunction messageBoxWithMessage:PASSWORD_CONFIRM_FAILED];
        return;
    }    
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    
    JddModifyPasswordOperation *operation = [[JddModifyPasswordOperation alloc] init];
    operation.delegate = self;
    operation.oldPassword = oldPwdTextField.text;
    operation.password = pwdTextField.text;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
   // [self.navigationItem setTitle:PAGE_MODIFY_PASSWORD];
    
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_MODIFY_PASSWORD];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
//        //self.navigationItem.hidesBackButton = YES;

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"XiuGaiMiMa"];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"XiuGaiMiMa"];
    [MobClick event:@"UserCenter-xiugaimima"];

    [oldPwdTextField resignFirstResponder];
    [pwdTextField resignFirstResponder];
    [confirmPwdTextField resignFirstResponder];
    oldPwdTextField.text = @"";
    pwdTextField.text = @"";
    confirmPwdTextField.text = @"";
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
